package com.mall.biz.user.record;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.mall.client.commkey.CommKeyService;
import com.mall.client.eshop.SaleEshopService;
import com.mall.client.item.MallItemPriceService;
import com.mall.client.item.MallItemService;
import com.mall.client.user.CollectionService;
import com.mall.client.user.UserService;
import com.mall.common.util.PageUtil;
import com.mall.enums.CollectionEnum;
import com.mall.enums.RegionEnum;
import com.mall.pojo.Collection;
import com.mall.pojo.CollectionExample;
import com.mall.pojo.CollectionExample.Criteria;
import com.mall.pojo.Commkey;
import com.mall.pojo.SaleEshop;
import com.mall.pojo.User;
import com.mall.query.Page;
import com.mall.query.collection.CollectionQuery;
import com.mall.vo.eshop.CollectionVO;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;

@Service
public class CollectionBizImpl {
	@Autowired
	private CollectionService collectionService;
	@Autowired
	private CommKeyService commKeyService;
	@Autowired
	private UserService userService;
	@Autowired
	private SaleEshopService saleEshopService;
	@Autowired
	private MallItemService mallItemService;
	@Autowired
	private MallItemPriceService mallItemPriceService;
	/**
	 * 收藏list
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	public List<Collection> collectionRecordMap(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize",PageUtil.PAGE_SIZE_APP);
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		Integer type = HttpUtil.getIntegerValueFromRequest(request, "type");
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		List<Collection> collectionList = this.getCollectionByUserIdAndOnlyKey(user.getId(),onlyKey,null,page,type,status);
		if(collectionList == null || collectionList.size() <= 0){
			return null;
		}
		return collectionList;
	}
	/**
	 * 去collection的list
	 * @param userId
	 * @param onlyKey
	 * @param mallId
	 * @param page
	 * @return
	 */
	public List<Collection> getCollectionByUserIdAndOnlyKey(Long userId, Integer onlyKey,Long mallId,Page page,Integer type,Integer status) {
		CollectionExample example = new CollectionExample();
		if(page != null){
			example.setOrderByClause("id desc");
			example.setLimit(page.getLength());
			example.setStart(page.getBegin());
		}
		Criteria criteria = example.createCriteria();
		if(userId != null && userId > 0){
			criteria.andUserIdEqualTo(userId);
		}
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		if(mallId != null && mallId >0){
			criteria.andMallIdEqualTo(mallId);
		}
		if(type > 0){
			criteria.andTypeEqualTo(type);
		}
		if(status != null ){
			criteria.andStatusEqualTo(status);
		}
		List<Collection> collectionList = this.collectionService.getCollectionByExample(example);
		return collectionList;
	}

	/**
	 * 收藏的action的biz
	 * @param userId
	 * @param onlyKey
	 * @param mallId
	 * @return
	 */
	public Collection collectionMallByMallIdAndOnlyKey(Long userId,Integer onlyKey, Long mallId,Integer type,Integer status) {
		List<Collection> collectionList = getCollectionByUserIdAndOnlyKey(userId,onlyKey,mallId,null,type,status);
		Collection collection  = new Collection();
		if(collectionList != null && collectionList.size()>0){
			collection = collectionList.get(0);
		}
		if(status == 0){
			collection.setStatus(4);
		}else if(status == 4  || status ==null){
			collection.setStatus(0);
		}
		collection.setGmtModify(new Date());
		if (collectionList == null) {
			List<Commkey> commKeyList = this.commKeyService.getComKeyByCommOnlyKey(onlyKey);
			collection.setCommOnlyKey(onlyKey);
			collection.setProvinceId(commKeyService.getRegionByList(RegionEnum.PROVINCE.getCode(), commKeyList));
			collection.setCityId(commKeyService.getRegionByList(RegionEnum.CITY.getCode(), commKeyList));
			collection.setAreaId(commKeyService.getRegionByList(RegionEnum.AREA.getCode(), commKeyList));
			collection.setMallId(mallId);
			collection.setUserId(userId);
			collection.setType(type);
			collection.setGmtCreate(new Date());
			this.collectionService.insertCollection(collection);
		} else {
			this.collectionService.modifyCollection(collection);
		}
		return collection;
	}
	
	
	public Integer addOrCancelCollection(Long userId,Integer onlyKey, Long mallId,Integer type,Integer status){
		List<Collection> collectionList = getCollectionByUserIdAndOnlyKey(userId,onlyKey,mallId,null,type,null);
		Collection collection  = new Collection();
		collection.setGmtModify(new Date());
		if(collectionList != null && collectionList.size()>0){
			collection = collectionList.get(0);
			collection.setStatus(status);
			return this.collectionService.modifyCollection(collection);
		}
		List<Commkey> commKeyList = this.commKeyService.getComKeyByCommOnlyKey(onlyKey);
		collection.setCommOnlyKey(onlyKey);
		collection.setProvinceId(commKeyService.getRegionByList(RegionEnum.PROVINCE.getCode(), commKeyList));
		collection.setCityId(commKeyService.getRegionByList(RegionEnum.CITY.getCode(), commKeyList));
		collection.setAreaId(commKeyService.getRegionByList(RegionEnum.AREA.getCode(), commKeyList));
		collection.setMallId(mallId);
		collection.setUserId(userId);
		collection.setType(type);
		collection.setStatus(status);
		collection.setGmtCreate(new Date());
		return this.collectionService.insertCollection(collection);
	}
	
	
	
	/**
	 * 判斷是否被收藏
	 * @param onlyKey
	 * @param mallId
	 * @param UserId
	 * @param type
	 * @param Status
	 * @return
	 */
	public Integer isCollection(Integer onlyKey,Long mallId,User user,Integer type){
		Integer status = CollectionEnum.UNCOLLECTION.getCode();
		if(user == null){
			return status;
		}
		CollectionQuery collectionQuery = new CollectionQuery();
		collectionQuery.setMallId(mallId);
		collectionQuery.setOnlyKey(onlyKey);
		collectionQuery.setType(type);
		collectionQuery.setUserId(user.getId());
		collectionQuery.setStatus(CollectionEnum.COLLECTION.getCode());
		Collection collection = this.collectionService.getCollectionByCollectionQuery(collectionQuery);
		
		if(collection != null){
			status = collection.getStatus();
		}
		
		return status;
	}
	
	/**
	 * 将collection集合转换成collectionvo集合
	 * @param collectionList
	 * @return
	 */
	public List<CollectionVO> getCollectionVOListByCollectionList(List<Collection> collectionList){
		if(collectionList == null){
			return null;
		}
		List<CollectionVO> collectionVOList = new ArrayList<CollectionVO>();
		for(Collection collection : collectionList){
			CollectionVO collectionVO = new CollectionVO();
			User user = this.userService.getUserByUserId(collection.getUserId());
			SaleEshop saleEshop = this.saleEshopService.getSaleEshopBySaleEshopId(collection.getMallId());
			collectionVO.setCollection(collection);
			collectionVO.setSaleEshop(saleEshop);
			collectionVO.setUser(user);
			collectionVO.setMallItem(this.mallItemService.getMallItemById(collection.getMallId()));
			if(this.mallItemPriceService.getMallItemPriceByLevelAndKey(user.getLevel().longValue(), collection.getCommOnlyKey(), collection.getMallId(),0) != null){
				collectionVO.setMallItemPrice(this.mallItemPriceService.getMallItemPriceByLevelAndKey(user.getLevel().longValue(), collection.getCommOnlyKey(), collection.getMallId(),0));
			}else{
				collectionVO.setMallItemPrice(this.mallItemPriceService.getMallItemPriceByLevelAndKey(1l, collection.getCommOnlyKey(), collection.getMallId(),0));
			}
			collectionVOList.add(collectionVO);
		}
		return collectionVOList;
	}
	
	
	public CollectionExample getCollectionExampleByQuery(CollectionQuery query){
		CollectionExample example = new CollectionExample();
		if(query.getPage() != null){
			example.setLimit(query.getPage().getLength());
			example.setStart(query.getPage().getBegin());
		}
		if(StringUtils.isNotBlank(query.getOrderBy())){
			example.setOrderByClause("id desc");
		}
		Criteria criteria = example.createCriteria();
		if(query.getUserId() != null && query.getUserId() > 0){
			criteria.andUserIdEqualTo(query.getUserId());
		}
		if(query.getOnlyKey() != null){
			criteria.andCommOnlyKeyEqualTo(query.getOnlyKey());
		}
		
		if(query.getMallId() != null && query.getMallId() >0){
			criteria.andMallIdEqualTo(query.getMallId());
		}
		if(query.getType() > 0){
			criteria.andTypeEqualTo(query.getType());
		}
		if(query.getStatus() != null ){
			criteria.andStatusEqualTo(query.getStatus());
		}
		return example;
	}

}
